from windoh.control import Control

class Slider(Control):
    class SliderHandle(Control):
        def on_mouse_press(self, x, y, b, m):
            self.start_drag()
        
    class VertSliderHandle(SliderHandle):
        def on_drag(self, x, y, dx, dy, b, m):
            cs = self._computed_style
            pcs = self._parent._computed_style
            height = pcs['client_height'] - cs['height']
            t = y - pcs['screen_y']# - pcs['client_y']
            print y, pcs['screen_y'], pcs['client_y']
            if t < 0:
                t = 0
            elif t > height:
                t = height

            v = (1.0 / height) * t

            self._parent.dispatch_event('on_value_change', v)
    
    class HorizSliderHandle(SliderHandle):
        def on_mouse_press(self, x, y, b, m):
            self.start_drag()

        def on_drag(self, x, y, dx, dy, b, m):
            cs = self._computed_style
            pcs = self._parent._computed_style
            width = pcs['client_width'] - cs['width']
            t = x - pcs['screen_x'] + pcs['client_x']
            if t < 0:
                t = 0
            elif t > width:
                t = width

            v = (1.0 / width) * t

            self._parent.dispatch_event('on_value_change', v)

    def __init__(self, value=0.0, direction="vert", **kwargs):
        Control.__init__(self, **kwargs)
        self._value = value
        self.direction = direction
        self._create_slider_handle()
        self._children_proxy = ()

    def _create_slider_handle(self):
        try:
            self._children.remove(self._handle)
        except AttributeError:
            pass

        if self.direction == 'vert':
            self._handle = self.VertSliderHandle(top=0, right=2, height=7, left=2)
        elif self.direction == 'horiz':
            self._handle = self.HorizSliderHandle(top=2, right=0, width=7, bottom=2)
        else:
            assert(False)

        self._children.append(self._handle)

        print self._children

    def __getstate__(self):
        d = Control.__getstate__(self)
        d['value'] = self._value
        return d

    def _get_value(self):
        return self._value
    def _set_value(self, v):
        self.dispatch_event('on_value_change', v)
    value = property(_get_value, _set_value)

    def on_value_change(self, v):
        self._value = v
        self.set_dirty()

    def on_compute_style(self):
        Control.on_compute_style(self)
        self._recalc_handle_pos()

    def _recalc_handle_pos(self):
        if self.direction == 'vert':
            self._handle.style['bottom'] = int((self._computed_style['client_height'] - 
                                       self._handle.style['height']) * 
                                       self._value)
        elif self.direction == 'horiz':
            self._handle.style['left'] = int((self._computed_style['client_width'] - 
                                       self._handle.style['width']) * 
                                       self._value)
        else:
            assert(False)

Slider.register_event_type('on_value_change')
        
        

   
